package com.ansion.commonchat.contact.adapter;

import android.view.View;

import androidx.annotation.NonNull;

import com.ansion.adapter.ITurboItem;
import com.ansion.adapter.TurboAdapter;
import com.ansion.baseui.base.view.UserFaceView;
import com.ansion.baseui.base.view.UserNameView;
import com.ansion.commonchat.R;
import com.ansion.commonchat.contact.bean.ContactBaseBean;
import com.ansion.commonchat.contact.listener.IOnItemClickListener;
import com.ansion.commonchat.contact.ui.ContactItemType;
import com.ansion.constant.BConstant;


/**
 * 联系人 数据
 */
public class ContactDataItem implements ITurboItem<ContactDataItem.VH> {

    private IOnItemClickListener listener;

    @Override
    public int getType() {
        return ContactItemType.ItemTypeData;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.contact_item_data;
    }

    public void addOnItemClickListener(IOnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public VH getViewHolder(View v) {
        return new VH(v);
    }

    class VH extends TurboAdapter.ViewHolder<ContactBaseBean> {
        UserFaceView mIvIcon;
        UserNameView mTvName;

        public VH(View itemView) {
            super(itemView);
            mIvIcon = itemView.findViewById(R.id.contact_item_data_icon);
            mTvName = itemView.findViewById(R.id.contact_item_data_name);
        }

        @Override
        public void bindView(ContactBaseBean bean, int position) {
            if (bean == null || bean.getUserModel() == null) {
                return;
            }
            String uid = bean.getUserModel().getUserId() + "";
            mIvIcon.bindUid(uid);
            mTvName.bindUid(uid);
            mTvName.setBadgeMpFrom(BConstant.MPFrom.FROM_BADGE_CONTACT_LIST);
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(bean.getUserModel());
                }
            });
        }
    }
}
